{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ec7c2a6e",
   "metadata": {},
   "source": [
    "# 金融市场基第３课书面作业\n",
    "学号：114847\n",
    "\n",
    "**作业内容：**  \n",
    "1. 一5年期的债券，面值1000元，每年支付利息80元，市场利率为6%，则此债券的市场价值应为多少？\n",
    "2. 上题中，如果持有2年半这只债券，此时市场利率变为4%，是否应该出售这只债券？如果是，应该以什么价格出售？\n",
    "3. 对于连续复利的一款投资产品，收益率为7%，问几年投资可以翻倍？\n",
    "4. 某银行使用5%的年利率（一年付息一次）用于某一个风险模型，如果想要转换成半年付息一次，对应的年利率应为多少？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68740b23",
   "metadata": {},
   "source": [
    "## 第1题\n",
    "一5年期的债券，面值1000元，每年支付利息80元，市场利率为6%，则此债券的市场价值应为多少？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6488d358",
   "metadata": {},
   "source": [
    "答：  \n",
    "根据公式：  \n",
    "$$\n",
    "FV = PV(1+y)^m\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "191af988",
   "metadata": {},
   "source": [
    "$FV=1000, m=5,y=0.06$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2bc66504",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PV= 747.2581728660571\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "FV = 1000\n",
    "m = 5\n",
    "y = 0.06\n",
    "PV = FV/math.pow(1+y,m)\n",
    "print('PV=',PV)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "301d8db1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1年利息：44.835490\n",
      "第2年利息：47.525620\n",
      "第3年利息：50.377157\n",
      "第4年利息：53.399786\n",
      "第5年利息：56.603774\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,6):\n",
    "    print('第%d年利息：%f'%(i,PV*math.pow(1+y,i-1)*y))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49280d9c",
   "metadata": {},
   "source": [
    "计算了一下，如果是折价债券，则发行价应该是747元。如下以747元改造，则每年的利息如下，都小于题目中的每年80元利息。因此题目中的债券应该是溢价债券。我们如下计算："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9a8b8da",
   "metadata": {},
   "source": [
    "第5年需要支持1000元+利息80元，共1080元，折算到今年的金额为A。  \n",
    "第4年需要支持利息80元，共80元，折算到今年的金额为B。  \n",
    "第3年需要支持利息80元，共80元，折算到今年的金额为C。  \n",
    "第2年需要支持利息80元，共80元，折算到今年的金额为D。  \n",
    "第1年需要支持利息80元，共80元，折算到今年的金额为E。  \n",
    "则此债券的市场价值T=A+B+C+D+E"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "79ce8e1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第5年折算到今年的金额,A= 807.0388266953415\n",
      "第4年折算到今年的金额,B= 63.367493059041635\n",
      "第5年折算到今年的金额,C= 67.16954264258415\n",
      "第4年折算到今年的金额,D= 71.19971520113918\n",
      "第4年折算到今年的金额,E= 75.47169811320754\n",
      "则此债券的市场价值T= 1084.247275711314\n"
     ]
    }
   ],
   "source": [
    "A = 1080/math.pow(1+y,5)\n",
    "B = 80/math.pow(1+y,4)\n",
    "C = 80/math.pow(1+y,3)\n",
    "D = 80/math.pow(1+y,2)\n",
    "E = 80/math.pow(1+y,1)\n",
    "print('第5年折算到今年的金额,A=',A)\n",
    "print('第4年折算到今年的金额,B=',B)\n",
    "print('第5年折算到今年的金额,C=',C)\n",
    "print('第4年折算到今年的金额,D=',D)\n",
    "print('第4年折算到今年的金额,E=',E)\n",
    "print('则此债券的市场价值T=',A+B+C+D+E)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2ff9810",
   "metadata": {},
   "source": [
    "## 第2题\n",
    "上题中，如果持有2年半这只债券，此时市场利率变为4%，是否应该出售这只债券？如果是，应该以什么价格出售？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "51e1f13f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calc_v(paylist,ratio):\n",
    "    n = len(paylist)\n",
    "    t = 0\n",
    "    for i in range(n):\n",
    "        t += paylist[i]/math.pow(1+ratio,i+1)\n",
    "    return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1e9e342c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "利率是6%时折算到2年期上的价格= 1053.4602389892327\n",
      "利率是4%时折算到2年期上的价格= 1111.003641329085\n"
     ]
    }
   ],
   "source": [
    "t1 = calc_v([80,80,1080],0.06)\n",
    "print(r'利率是6%时折算到2年期上的价格=',t1)\n",
    "\n",
    "t2 = calc_v([80,80,1080],0.04)\n",
    "print(r'利率是4%时折算到2年期上的价格=',t2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45188472",
   "metadata": {},
   "source": [
    "从这个角度看，利率下降债券的价格下降了，因此不应该出售这只债券。如果非要出售，价格上还要加上半年的利息："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "31f2f76f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "建议的出售价格= 1133.2237141556668\n"
     ]
    }
   ],
   "source": [
    "t2=calc_v([80,80,1080],0.04)\n",
    "t2=t2*(1+0.04/2)\n",
    "print(r'建议的出售价格=',t2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0d2d9ad",
   "metadata": {},
   "source": [
    "## 第3题\n",
    "对于连续复利的一款投资产品，收益率为7%，问几年投资可以翻倍？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bccfafbc",
   "metadata": {},
   "source": [
    "答：  \n",
    "其实就是解如下方程  \n",
    "$$\n",
    "2a = a(1+0.07)^x \\\\\n",
    "\\ln{(2)} = x \\ln{(1.07)} \\\\\n",
    "x=\\frac{\\ln{(2)}}{\\ln{(1.07)}}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "15e1129a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "需要10年投资可以翻倍\n"
     ]
    }
   ],
   "source": [
    "print('需要%d年投资可以翻倍'%(math.log(2)/math.log(1.07)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77664db8",
   "metadata": {},
   "source": [
    "## 第4题\n",
    "某银行使用5%的年利率（一年付息一次）用于某一个风险模型，如果想要转换成半年付息一次，对应的年利率应为多少？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7142a2a5",
   "metadata": {},
   "source": [
    "答：  \n",
    "假设要求解的年利率为$2x$，对应半年利率为$x$。  \n",
    "假设$PV=a$，投资期为$m$年，则：\n",
    "$$\n",
    "a(1+0.05)^m = a(1+x)^{2m} \\\\\n",
    "1.05 = (1+x)^2 \\\\\n",
    "x^2+2x-0.05=0\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8daffd77",
   "metadata": {},
   "source": [
    "根据二元一次方程的求根公式，可以计算得：\n",
    "$$\n",
    "x=\\frac{-2\\pm \\sqrt{4.2}}{2} \\\\\n",
    "2x=-2\\pm \\sqrt{4.2}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e12650a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.04939015319191986 -4.04939015319192\n"
     ]
    }
   ],
   "source": [
    "print(-2+math.sqrt(4.2), -2-math.sqrt(4.2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac046466",
   "metadata": {},
   "source": [
    "-4.049的解不符合业务，因此对应的年利率应该是4.94%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c513671f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
